<template>
  <div class="mod-config">
    <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      size="small"
      :visible.sync="visible"
      :append-to-body="true"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="150px"
      >
        <el-form-item label="spu_id" prop="skuId">
          <el-input v-model="dataForm.skuId" placeholder="spu_id"></el-input>
        </el-form-item>
        <el-form-item label="满多少" prop="fullPrice">
          <el-input-number
            v-model="dataForm.fullPrice"
            controls-position="right"
            :precision="2"
            :step="0.1"
            :min="0"
            label="满多少"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="减多少" prop="reducePrice">
          <el-input-number
            v-model="dataForm.reducePrice"
            controls-position="right"
            :precision="2"
            :step="0.1"
            :min="0"
            label="减多少"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="是否参与其他优惠" prop="addOther">
          <el-switch
            v-model="dataForm.addOther"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveOrUpdateSkuFull } from "@/api/coupon";

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        skuId: "",
        fullPrice: "",
        reducePrice: "",
        addOther: 0,
      },
      dataRule: {
        skuId: [{ required: true, message: "spu_id不能为空", trigger: "blur" }],
        fullPrice: [
          { required: true, message: "满多少不能为空", trigger: "blur" },
        ],
        reducePrice: [
          { required: true, message: "减多少不能为空", trigger: "blur" },
        ],
        addOther: [
          {
            required: true,
            message: "是否参与其他优惠不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(data) {
      this.dataForm.id = 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.id = data.id;
          this.dataForm.skuId = data.skuId;
          this.dataForm.fullPrice = data.fullPrice;
          this.dataForm.reducePrice = data.reducePrice;
          this.dataForm.addOther = data.addOther + "";
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveOrUpdateSkuFull({
            id: this.dataForm.id || "",
            skuId: this.dataForm.skuId,
            fullPrice: this.dataForm.fullPrice,
            reducePrice: this.dataForm.reducePrice,
            addOther: this.dataForm.addOther,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
